<?php View::render('admin/layouts/meta');?>
<style>
    .j-u-del1{position:absolute;right:-0.6rem;top:-0.6rem;font-size:1.2rem;color:#dd514c;margin-top: 8px;}
    .j-u-del1:hover{text-decoration: none;color:#dd514c;}
</style>
<link rel="stylesheet" type="text/css" href="<?php echo Router::getBaseUrl(); ?>/assets/js/webuploader.css" />
<body>
<article class="page-container">
    <form action="" method="post" class="form form-horizontal" id="form-member-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>投票问题：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<?php echo $info ? $info['problem'] : ''; ?>" placeholder="" name="problem">
            </div>
        </div>






        <?php if (!empty($voteOption)) {
            foreach ($voteOption as $key => $val) {
                ?>

                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3">
                        <?php if ($key == 0) {?>
                            <span class="c-red"></span>投票选项：
                        <?php }?>
                    </label>


                    <div class="formControls col-xs-8 col-sm-8">
                        <input type="text" class="input-text problem" value="<?php echo $val['vote_option']; ?>" placeholder="投票选项" name="option[]">
                    </div>

                    <div class="formControls col-xs-1 col-sm-1">
                        <input type="hidden" class="input-text option_id" value="<?php echo $val['id']; ?>" name="option_id[]">
                    </div>
                </div>
            <?php }} else {
            ?>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3"><span class="c-red"></span>投票选项：</label>

                <div class="formControls col-xs-8 col-sm-8">
                    <input type="text" class="input-text option" value="" placeholder="投票选项" name="option[]">
                </div>
                <div class="formControls col-xs-1 col-sm-1">
                    <input type="hidden" class="input-text option_id" value="<?php echo $val['id']; ?>" name="option_id[]">
                </div>

            </div>
        <?php }?>
        <div class="row cl" id="attributes">
            <label class="form-label col-xs-4 col-sm-3"></label>
            <div class="formControls col-xs-1 col-sm-1">
                <input class="btn btn-primary radius" type="button" id="g_submit" value="&nbsp;&nbsp;继续添加&nbsp;&nbsp;">
            </div>
        </div>

        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input type="hidden" name="voteId" value="<?php echo $info ? $info['id'] : 0; ?>" />
                <input class="btn btn-primary radius" type="button" id="j_submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>
</body>
<?php View::render('admin/layouts/footer');?>
<script type="text/javascript" src="<?php echo Router::getBaseUrl(); ?>/lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="<?php echo Router::getBaseUrl(); ?>/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="<?php echo Router::getBaseUrl(); ?>/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="<?php echo Router::getBaseUrl(); ?>/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="<?php echo Router::getBaseUrl(); ?>/lib/ueditor/1.4.3/ueditor.config.js?v=123"></script>
<script type="text/javascript" src="<?php echo Router::getBaseUrl(); ?>/lib/ueditor/1.4.3/ueditor.all.min.js?v=123"> </script>
<script type="text/javascript" src="<?php echo Router::getBaseUrl(); ?>/lib/ueditor/1.4.3/lang/zh-cn/zh-cn.js?v=123"></script>
<script type="text/javascript" src="<?php echo Router::getBaseUrl(); ?>/assets/js/webuploader.min.js"></script>
<script type="text/javascript">
    $(function(){
        formData = {}
        $.getJSON('/oss/getImageToken', function(ret){
            var data = ret.data;
            formData = {
                'key' : data.dir,
                'policy': data.policy,
                'OSSAccessKeyId': data.accessid,
                'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
                'callback' : '',
                'signature': data.signature,
                'domain': data.domain,
                'host':data.host
            };
        });
        $('#j_submit').click(function(){
            var $btn = $(this);
            $btn.button('loading');
            $.post('<?php echo Router::getBaseUrl(); ?>/admin_vote/edit', $('form').serialize(), function (result) {
                if (result.status == 200) {
                    layer.msg(result.msg, {
                        icon: 1,
                        skin: 'u-msg',
                        time: 1800,
                    }, function(){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    });
                    $btn.button('reset');
                    return true;
                }
                layer.msg(result.msg, {
                    icon: 0,
                    skin: 'u-msg',
                    time: 3000,
                }, function () {

                });
                $btn.button('reset');
            }, 'json');
            return;
        });
        var max_file = 1000, $list = $('#j-file');
        var uploader = WebUploader.create({
            auto:true,
            swf: '<?php echo Router::getBaseUrl(); ?>/assets/js/Uploader.swf',
            server: '<?php echo Router::getBaseUrl(); ?>/admin_upload/file',
            pick: '#j-upload',
            formData:formData,
            resize: true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            }
        });
        uploader.on('beforeFileQueued', function(file){
            formData.key += random_string(15)+get_suffix(file.name);
            uploader.options.server = formData.host;
            uploader.options.formData = formData;
        });

        uploader.on( 'uploadError', function( file ) {
            layer.msg('文件上传出错', {
                icon: 0,
                skin: 'u-msg',
                time: 3000,
            });
        });

        uploader.on( 'uploadSuccess', function( file,response) {
            img = formData.domain+'/'+formData.key;
            var tpl='<div class="f-l mr-10 j-u-box">\
                    <a href="javascript:;" class="j-u-del"><i class="Hui-iconfont">&#xe6dd;</i></a>\
                    <input type="hidden" name="image" value="'+img+'" />\
                    <img class="thumbnail1" src="'+img+'" style="width:100px;height:100px" />\
                </div>';
            $list.html(tpl);
            $('.j-u-del').on('click', function(){
                $(this).parent('div').remove();
            });
        });
        var max_file = 1000, $list1 = $('#j-file-thumbnail');
        var uploaders = WebUploader.create({
            auto:true,
            swf: '<?php echo Router::getBaseUrl(); ?>/assets/js/Uploader.swf',
            server: '<?php echo Router::getBaseUrl(); ?>/admin_upload/file',
            pick: '#j-upload-thumbnail',
            formData:formData,
            resize: true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            }
        });
        uploaders.on('beforeFileQueued', function(file){
            formData.key += random_string(15)+get_suffix(file.name);
            uploaders.options.server = formData.host;
            uploaders.options.formData = formData;
        });

        uploaders.on( 'uploadError', function( file ) {
            layer.msg('文件上传出错', {
                icon: 0,
                skin: 'u-msg',
                time: 3000,
            });
        });

        uploaders.on( 'uploadSuccess', function( file,response) {
            img = formData.domain+'/'+formData.key;
            var tpl='<div class="f-l mr-10 j-u-box">\
                    <a href="javascript:;" class="j-u-del"><i class="Hui-iconfont">&#xe6dd;</i></a>\
                    <input type="hidden" name="thumbnail" value="'+img+'" />\
                    <img class="thumbnail" src="'+img+'" style="width:100px;height:100px" />\
                </div>';
            $list1.html(tpl);
            $('.j-u-del').on('click', function(){
                $(this).parent('div').remove();
            });
        });
        $('.j-u-del').on('click', function(){
            $(this).parent('div').remove();
        });
        var max_file = 100, $list_avatar = $('#j-file-avatar');
        var uploader_avatar = WebUploader.create({
            auto:true,
            swf: '<?php echo Router::getBaseUrl(); ?>/assets/js/Uploader.swf',
            server: '<?php echo Router::getBaseUrl(); ?>/admin_upload/file',
            pick: '#j-upload-avatar',
            formData:formData,
            resize: true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            }
        });
        uploader_avatar.on('beforeFileQueued', function(file){
            formData.key += random_string(15)+get_suffix(file.name);
            uploader_avatar.options.server = formData.host;
            uploader_avatar.options.formData = formData;
        });

        uploader_avatar.on( 'uploadError', function( file ) {
            layer.msg('文件上传出错', {
                icon: 0,
                skin: 'u-msg',
                time: 3000,
            });
        });

        uploader_avatar.on( 'uploadSuccess', function( file,response) {
            img = formData.domain+'/'+formData.key;
            var tpl='<div class="f-l mr-10 j-u-box">\
                    <a href="javascript:;" class="j-u-del"><i class="Hui-iconfont">&#xe6dd;</i></a>\
                    <input type="hidden" name="image_list[]" value="'+img+'" />\
                    <img class="thumbnail" src="'+img+'" style="width:100px;height:100px" />\
                </div>';
            $list_avatar.append(tpl);
            $('.j-u-del').on('click', function(){
                $(this).parent('div').remove();
            });
        });

        var ue = UE.getEditor('editor');
    });

</script>
<script>
    // $(function(){
    //     $('.j-u-del1').on('click', function(){
    //         alert(111111);
    //     });
    // })
    function delattributes(obj,id) {
        if(id == 0){
            $(obj).parent().remove();
        }else{
            $.post('<?php echo Router::getBaseUrl(); ?>/admin_goods/deleteSpecification',{id: id}, function (result) {
                if (result.status == 200) {
                    $(obj).parent().remove();
                    layer.msg(result.msg, {
                        icon: 1,
                        skin: 'u-msg',
                        time: 1800,
                    }, function(){

                    });
                }
                layer.msg(result.msg, {
                    icon: 0,
                    skin: 'u-msg',
                    time: 3000,
                }, function () {

                });
            }, 'json');
        }

    }
    $('#g_submit').on('click', function(){
        var html='<div class="row cl">'
        html+='    <label class="form-label col-xs-4 col-sm-3"></label>'

        html+='    <div class="formControls col-xs-8 col-sm-8">'
        html+='        <input type="text" class="input-text option" value="" placeholder="投票选项" name="option[]">'
        html+='    </div>'

        html+='    <div class="formControls col-xs-1 col-sm-1" style="width: 40px;" onclick="delattributes(this,0)">'
        html+='        <a href="javascript:;" class="j-u-del1"><i class="Hui-iconfont">&#xe6dd;</i></a>'
        html+='    </div>'
        html+='    <div class="formControls col-xs-1 col-sm-1">'
        html+='        <input type="hidden" class="input-text option_id" value="0" name="option_id[]">'
        html+='    </div>'
        html+='</div>'
        $('#attributes').before(html)
    });
</script>
</html>